<?php
session_start();

require_once "includes/db.php";
require_once "class.userinfo.php";

if (!isset($_GET['name']))
	die("GEEN ZOEKTERM - PLACEHOLDER ERROR"); // geen zoekterm opgegeven
	
if (strlen($_GET["name"]) < 3) 
	die("TE WEINIG TEKENS OPGEGEVEN - DIT IS EEN PLACEHOLDER ERROR");  // te weinig tekens opgegeven

$search_term = mysql_real_escape_string(str_replace(array("%20", "+"), " ", $_GET['name']));

$search_terms = split(" ", $search_term);

if (count($search_terms) == 1)
{
	$sql = "SELECT `user_id`, `user_first_name`, `user_last_name` FROM `users`
			WHERE user_first_name LIKE '%{$search_term}%' OR user_last_name LIKE '%{$search_term}%'";
}
else
{
	$sql = "SELECT `user_id`, `user_first_name`, `user_last_name` FROM `users`
			WHERE user_first_name LIKE '%{$search_terms[0]}%' OR user_last_name LIKE '%{$search_terms[1]}%'";
}
$result = mysql_query($sql);

if (!$result)
	die("6"); // technical SQL error D:

$users = array();	
	
while ($row = mysql_fetch_array($result)) 
{
	$users[] = new UserInfo($row["user_id"], $row["user_first_name"], $row["user_last_name"]);
}

echo json_encode($users);
?>